/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FileSelectionPanel.java
 *
 * Created on Jul 22, 2009, 8:31:46 PM
 */

package org.afraid.poison.db2php.nb;

import java.io.File;
import java.net.URISyntaxException;
import javax.swing.JFileChooser;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileStateInvalidException;
import org.openide.util.Exceptions;

/**
 *
 * @author poison
 */
public class FileSelectionPanel extends javax.swing.JPanel {
	private File defaultDirectory;
	private File directory;
	private WizardDescriptor wizard;

    /** Creates new form FileSelectionPanel */
    public FileSelectionPanel(WizardDescriptor wizard) {
		setWizard(wizard);
        initComponents();
    }

	public FileSelectionPanel() {
		this(null);
	}


	/**
	 * @return the wizard
	 */
	public WizardDescriptor getWizard() {
		return wizard;
	}

	/**
	 * @param wizard the wizard to set
	 */
	public void setWizard(WizardDescriptor wizard) {
		this.wizard=wizard;
		if (null!=wizard) {
			setDefaultDirectory(null);
			getDefaultDirectory();
		}
	}

	/**
	 * @return the defaultDirectory
	 */
	public synchronized File getDefaultDirectory() {
		if (null==getWizard()) {
			setDefaultDirectory(new File(System.getProperty("user.home")));
		} else if (null==defaultDirectory) {
			FileObject targetFolder=Templates.getTargetFolder(getWizard());
			if (null!=targetFolder) {
				try {
					setDefaultDirectory(new File(targetFolder.getURL().toURI()));
				} catch (FileStateInvalidException ex) {
					Exceptions.printStackTrace(ex);
				} catch (URISyntaxException ex) {
					Exceptions.printStackTrace(ex);
				}
			}
			if (null==defaultDirectory) {
				Project project=Templates.getProject(getWizard());
				if (null!=project) {
					FileObject projectDirectory=project.getProjectDirectory();
					if (null!=projectDirectory) {
						try {
							setDefaultDirectory(new File(projectDirectory.getURL().toURI()));
						} catch (FileStateInvalidException ex) {
							Exceptions.printStackTrace(ex);
						} catch (URISyntaxException ex) {
							Exceptions.printStackTrace(ex);
						}
					}
				}
			}
			if (null==defaultDirectory) {
				setDefaultDirectory(new File(System.getProperty("user.home")));
			}
		}
		return defaultDirectory;
	}

	/**
	 * @param defaultDirectory the defaultDirectory to set
	 */
	public void setDefaultDirectory(File defaultDirectory) {
		this.defaultDirectory=defaultDirectory;
		if (null!=this.defaultDirectory && null==directory && null!=getDestinationDirectory()) {
			getDestinationDirectory().setText(this.defaultDirectory.getAbsolutePath());
		}
	}

	/**
	 * @return the directory
	 */
	public synchronized File getDirectory() {
		if (null==directory) {
			return getDefaultDirectory();
		}
		return directory;
	}

	/**
	 * @param directory the directory to set
	 */
	public synchronized void setDirectory(File directory) {
		this.directory=directory;
		getDestinationDirectory().setText(directory.getAbsolutePath());
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonBrowse = new javax.swing.JButton();
        destinationDirectory = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        buttonBrowse.setText(org.openide.util.NbBundle.getMessage(FileSelectionPanel.class, "FileSelectionPanel.buttonBrowse.text")); // NOI18N
        buttonBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowseActionPerformed(evt);
            }
        });

        destinationDirectory.setEditable(false);
        destinationDirectory.setText(getDirectory().getAbsolutePath());
        destinationDirectory.setToolTipText(org.openide.util.NbBundle.getMessage(FileSelectionPanel.class, "FileSelectionPanel.destinationDirectory.toolTipText")); // NOI18N
        destinationDirectory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                destinationDirectoryActionPerformed(evt);
            }
        });

        jLabel2.setText(org.openide.util.NbBundle.getMessage(FileSelectionPanel.class, "FileSelectionPanel.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(destinationDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonBrowse)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(destinationDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(buttonBrowse))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void buttonBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowseActionPerformed
		JFileChooser fChooser=new JFileChooser(getDirectory());
		fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fChooser.setMultiSelectionEnabled(false);
		if (JFileChooser.APPROVE_OPTION==fChooser.showOpenDialog(buttonBrowse)) {
			setDirectory(fChooser.getSelectedFile());
		}
}//GEN-LAST:event_buttonBrowseActionPerformed

	private void destinationDirectoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_destinationDirectoryActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_destinationDirectoryActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBrowse;
    private javax.swing.JTextField destinationDirectory;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

	/**
	 * @return the destinationDirectory
	 */
	private javax.swing.JTextField getDestinationDirectory() {
		return destinationDirectory;
	}

}
